<?php
/**
 * Created by PhpStorm.
 * User: cth
 * Date: 2017/6/13
 * Time: 上午10:13
 */

namespace App\Repositories;

use App\Models\Shop;
use App\Models\ShopRoomTable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class RoomTableRepository
{
    protected $shopRoomTable;

    public function __construct(ShopRoomTable $shopRoomTable)
    {
        $this->shopRoomTable = $shopRoomTable;
    }

    //新建/修改房桌
    public function createRoomTable($request)
    {
        $user_id = Auth::id();
        if(!$user_id){
            return show('201','用户未登录');
        }
        $shop = Shop::where('user_id',$user_id)
            ->where('id',$request->shop_id)
            ->get();
        if($shop->isEmpty()){
            return show('202','该用户未拥有商铺');
        }
        //插入/更新
        $data = [
            'shop_id'=>$request->shop_id,
            'type'=>$request->type,
            'hold'=>$request->hold,
            'name'=>$request->name,
            'min_consumption'=>$request->min_consumption,
            'lock_qrcode'=>$request->lock_qrcode,
            'thumb'=>$request->thumb
        ];
        shopRoomTable::create($data);
        return show('200','创建房桌成功');
    }
    public function updateRoomTable($request)
    {
        try{
            $is_yours = ShopRoomTable::where('shop_id',$request->shop_id)->where('id',$request->id)->count();
            if (!$is_yours){
                return show(202,'没有权限');
            }
            $user_id = Auth::id();
            if(!$user_id){
                return show('201','用户未登录');
            }
            $shop = Shop::where('user_id',$user_id)
                ->where('id',$request->shop_id)
                ->get();
            if($shop->isEmpty()){
                return show('202','该用户未拥有商铺');
            }
            //插入/更新g
    //        $request = $request->toArray();
            $data = [
                'type' =>$request->type,
                'name'=>$request->name,
                'hold'=>$request->hold,
                'min_consumption'=>$request->min_consumption,
                'lock_qrcode'=>$request->lock_qrcode,
                'thumb'=>$request->thumb
            ];

            DB::table('shop_room_tables')->where('id',$request->id)->update($data);
//        dd($res);
//            if (!$res){
//                return show('201','修改房桌失败');
//            }
            return show('200','修改房桌成功');
        }catch (\Exception $exception){
            dd($exception->getMessage());
            return show('201','修改房桌失败');
        }


    }

    //删除房间桌子
    public function deleteRoomTable($id)
    {

        $user_id = Auth::id();
        $ShopRoomTable = ShopRoomTable::where('id',$id)->first();
        if($ShopRoomTable==null){
            return show(203,'id传参错误');
        }
        if($ShopRoomTable->is_use==1){
            return show(202,'该房间/桌子正在使用中，不可删除');
        }
        $shop = Shop::where('user_id',$user_id)
            ->where('id',$ShopRoomTable->shop_id)
            ->first();
        if(!$shop){
            return show(204,'该房间/桌子不属于该商家');
        }
        $data = ShopRoomTable::where('id',$id)->delete();
        if(!$data)
        {
            return show(201,'删除房桌失败');
        }
        return show(200,'删除房桌成功');
    }

    //房间桌子展示
    public function showRoomTables($shop_id)
    {
        $data = ShopRoomTable::where('shop_id',$shop_id)->get();
        if($data->isEmpty())
        {
            return show(201,'房桌数据为空');
        }

        foreach ($data as $dt){
            if (strpos($dt->thumb,';')){
                $res = explode(';',$dt->thumb);
                $dt->thumb=$res[0];
            }
        }
        return show(200,'房桌数据获取成功',$data);
    }

    public function showOne($id)
    {
        try{
            $data = ShopRoomTable::select(['id','name','type','note','hold','is_use','min_consumption','thumb','created_at'])->find($id);
            if(!$data)
            {
                return show(201,'房桌数据为空');
            }
        }catch (\Exception $e){
            return show(201,'房桌数据为空');
        }
        return show(200,'房桌数据获取成功',$data);
    }
}
